package cn.dinkevin.xui.manager;

import android.content.Context;
import android.text.InputFilter;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

/**
 * 输入法管理工具辅助类
 * Created by ChengPengFei on 2017/3/3 0003.</br>
 * Copyright ©2017 juziwl, All Rights Reserved.
 */

public final class InputManager {

    private InputManager(){}

    /**
     * 打开软键盘
     * @param context
     * @param edt_input
     */
    public static void showSoftInput(Context context, EditText edt_input){
        edt_input.setFocusable(true);
        edt_input.setFocusableInTouchMode(true);
        edt_input.requestFocus();
        InputMethodManager input = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        input.toggleSoftInput(0,InputMethodManager.SHOW_FORCED);
    }

    /**
     * 关闭软键盘
     * @param context
     * @param edt_input
     */
    public static void closeSoftInput(Context context, EditText edt_input){
        if(edt_input == null && !edt_input.isFocused()){
            return;
        }
        edt_input.clearFocus();
        InputMethodManager input = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        input.hideSoftInputFromWindow(edt_input.getWindowToken(),0);
    }

    /**
     * 设置 EditText 的最大输入文字长度
     * @param edt_input
     * @param maxLength
     */
    public static void setMaxInputLength(EditText edt_input,int maxLength){
        if(edt_input == null || maxLength < 1) return;
        edt_input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxLength)});
    }
}
